package hms.inventory;

import hms.master.MasterController;
import java.sql.*;

import javax.swing.DefaultListModel;

/**
 * An ItemCatalog is a collection of item names.
 * 
 * @author Akilah McIntyre
 */
public class ItemCatalog extends DefaultListModel {
	/**
	 * // TODO [Akilah] Add Comments
	 */
	public ItemCatalog() {
	}

	/**
	 * Initialize item catalog from database
	 */
	public void initItemCatlalog() {
		int catlg_item_id = -1; // unique identifier
		String catlg_item_name = ""; // name
		String size_count = "select count(*) from ITEM_NAME where ITEM_NAME_ID =99 and ITEM_NAME='UNDEFINED'";
        int undfCount = 0;
        try {
        	ResultSet rsCount = MasterController.getDatabaseObject().getResult(size_count);
        	while(rsCount.next()){
        		undfCount = rsCount.getInt(1);
        		if(undfCount == 0){
        			MasterController.getDatabaseObject().updateCommand("INSERT INTO ITEM_NAME(ITEM_NAME_ID,ITEM_NAME) VALUES(99,'UNDEFINED')");
        		}
        	}
    		// get data from database associated with dao
		ResultSet rsItemCatlalog = MasterController
					.getDatabaseObject()
					.getResult(
							"SELECT ITEM_NAME_ID, ITEM_NAME FROM ITEM_NAME WHERE ITEM_NAME_ID != 99 ORDER BY ITEM_NAME_ID");
			// Create CatalogItem instance from database
			while (rsItemCatlalog.next()) {
				catlg_item_id = rsItemCatlalog.getInt("ITEM_NAME_ID");
				catlg_item_name = rsItemCatlalog.getString("ITEM_NAME");
				CatalogItem catlgItem = new CatalogItem(catlg_item_id,
						catlg_item_name);
				// add to CatalogItem model
				this.addElement(catlgItem);
			}
		} catch (SQLException e) {
			// TODO [Akilah] Write to view or popup dialog
			e.printStackTrace();
		}
	}

	/**
	 * Adds catalog item
	 * 
	 * @param next_catlg_item_id
	 *            unique identifier
	 * @param new_catlg_item_name
	 *            name of new catalog item
	 */
	public void addCatalogItem(int next_catlg_item_id,
			String new_catlg_item_name) {
		this.addElement(new CatalogItem(next_catlg_item_id, new_catlg_item_name));
	}

	/**
	 * Adds catalog item
	 * 
	 * @param new_catlg_item_name
	 *            name of new catalog item
	 */
	public boolean addDbItemCatalog(String new_catlg_item_name) {
		boolean isSuccess = false;
		// Get new id as new id may not be +1
		int next_catlg_item_id = MasterController.getDatabaseObject()
				.addDBTableValueParam("ITEM_NAME", "ITEM_NAME", "ITEM_NAME_ID",
						new_catlg_item_name);

		if (next_catlg_item_id > -1) { // if the insertion was successful
			this.addElement(new CatalogItem(next_catlg_item_id,
					new_catlg_item_name));
			isSuccess = true;
		}

		return isSuccess;
	}

	/**
	 * @param catlg_item_name
	 *            New Name of ItemCatalog
	 * @param selIndex
	 *            Index of target catlg_item
	 * @return if database update is success
	 */
	public boolean updateDbItemCatalog(String catlg_item_name, int selIndex) {
		boolean isSuccess = false;
		int catlg_item_id = -1;
		try {
			catlg_item_id = ((CatalogItem) this.get(selIndex)).catlg_item_id;
			isSuccess = MasterController.getDatabaseObject()
					.updateDBTableValueParam("ITEM_NAME", "ITEM_NAME",
							"ITEM_NAME_ID", catlg_item_name, catlg_item_id);
			if (isSuccess) {
				this.changeItemCatalogName(catlg_item_name, selIndex);
			}
		} catch (Exception ex) {
			isSuccess = false;
		}
		return isSuccess;
	}

	/**
	 * @param catlg_item_name
	 *            New Name of ItemCatalog
	 * @param selIndex
	 *            Index of target catlg_item
	 */
	public void changeItemCatalogName(String catlg_item_name, int selIndex) {
		CatalogItem destCatalogItem = ((CatalogItem) this.get(selIndex));
		destCatalogItem.catlg_item_name = catlg_item_name;
	}

	public boolean deleteDbItemCatalog(String catlg_item_name, int selIndex) {
		boolean isSuccess = false;
		int catlg_item_id = -1;
		try {
			catlg_item_id = ((CatalogItem) this.get(selIndex)).catlg_item_id;
			MasterController.getDatabaseObject().updateCommand("UPDATE INVENTORY SET ITEM_NAME_ID = 99 WHERE ITEM_NAME_ID = " + catlg_item_id);
			isSuccess = MasterController.getDatabaseObject()
					.deleteDBTableValueParam("ITEM_NAME", "ITEM_NAME",
							"ITEM_NAME_ID",
							catlg_item_name, catlg_item_id);
			if (isSuccess) {
				this.deleteItemCatalogName(selIndex);
			}
		} catch (Exception ex) {
			isSuccess = false;
		}
		return isSuccess;
	}

	/**
	 * @param selIndex
	 *            Index of target size
	 */
	public void deleteItemCatalogName(int selIndex) {
		this.remove(selIndex);
		
	}
}